package com.shouhuobao.take;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

import com.iseastar.BaseActivity2;
import com.shouhuobao.station.R;

import java.lang.reflect.Field;

/**
 * @author coffee <br>
 *         2016-9-8上午11:24:00
 */
public abstract class BaseActivityWithScroll extends BaseActivity2 implements ViewTreeObserver.OnGlobalLayoutListener {
    /**
     * 根布局
     */
    private ViewGroup mContentLayout;
    // 状态栏的高度
    private int statusBarHeight;
    // 软键盘的高度
    private int keyboardHeight;
    // 软键盘的显示状态
    private boolean isShowKeyboard;
    //
    private int scrollHeight = 300;// px

    @Override
    protected void findViewById() {
        super.findViewById();
        mContentLayout = (ViewGroup) findViewById(R.id.content_layout);
        if (mContentLayout == null) {
            mContentLayout = (ViewGroup) findViewById(R.id.root_layout);
        }
        if (mContentLayout != null) {
            mContentLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
            statusBarHeight = getStatusBarHeight(this);
        } else {
            // 如果没设置content_layout或者root_layout 则不作处理
        }
    }

    /***
     * 监控布局变化
     */
    @Override
    public void onGlobalLayout() {
        // 应用可以显示的区域。此处包括应用占用的区域，
        // 以及ActionBar和状态栏，但不含设备底部的虚拟按键。
        Rect r = new Rect();
        mContentLayout.getWindowVisibleDisplayFrame(r);

        // 屏幕高度。这个高度不含虚拟按键的高度
        int screenHeight = mContentLayout.getRootView().getHeight();

        int heightDiff = screenHeight - (r.bottom - r.top);

        // 在不显示软键盘时，heightDiff等于状态栏的高度
        // 在显示软键盘时，heightDiff会变大，等于软键盘加状态栏的高度。
        // 所以heightDiff大于状态栏高度时表示软键盘出现了，
        // 这时可算出软键盘的高度，即heightDiff减去状态栏的高度
        if (keyboardHeight == 0 && heightDiff > statusBarHeight) {
            keyboardHeight = heightDiff - statusBarHeight;
        }

        final int height = getScrollHeight();
        if (isShowKeyboard) {// 软键盘弹出的状态
            // 如果软键盘是弹出的状态，并且heightDiff小于等于状态栏高度，
            // 说明这时软键盘已经收起
            if (heightDiff <= statusBarHeight) {
                isShowKeyboard = false;
                // 键盘弹出，布局整体上移
                if (mContentLayout instanceof ScrollView) {
                    ScrollView scrollView = (ScrollView) mContentLayout;
                    scrollView.smoothScrollBy(0, -height);
                } else {
                    ObjectAnimator animator = ObjectAnimator.ofFloat(mContentLayout, "translationY", -height, 0);
                    animator.start();
                }
            }
        } else {
            // 如果软键盘是收起的状态，并且heightDiff大于状态栏高度，
            // 说明这时软键盘已经弹出
            if (heightDiff > statusBarHeight) {
                isShowKeyboard = true;
                if (mContentLayout instanceof ScrollView) {
                    final ScrollView scrollView = (ScrollView) mContentLayout;
                    getHandler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.smoothScrollBy(0, height);
                        }
                    },100);
                } else {
                    // 键盘收起，布局整体下移
                    ObjectAnimator animator = ObjectAnimator.ofFloat(mContentLayout, "translationY", 0, -height);
                    animator.start();
                }
            }
        }
    }

    /**
     * @param scrollHeight 单位是dp 不是px
     */
    public void setScrollHeight(int scrollHeight) {
        this.scrollHeight = (int) (scrollHeight * getResources().getDisplayMetrics().density);
    }

    protected int getScrollHeight() {
        return scrollHeight;
    }

    @SuppressLint("NewApi")
    @Override
    protected void onDestroy() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            mContentLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            mContentLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        super.onDestroy();
    }

    /***
     * 获取状态栏的高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
